package com.spudtech
{
    
    import flash.geom.*;
    import flash.display.*;
    
    import com.spudtech.collision.*;
    import com.spudtech.graphics.*;
    
    
    public class SpudActor extends SpudEntity
    {
        
        protected var _velX : Number;
        protected var _velY : Number;
        
        protected var _maxVelX : Number;
        protected var _maxVelY : Number;
        
        
        public function SpudActor(position : Point = null,
                                  max_vel : Point = null,
                                  init_graphic : ISpudDrawable = null,
                                  init_collider : SpudCollider = null,
                                  graphic_offset : Point = null,
                                  collider_offset : Point = null)
        {
            super(position,init_graphic,init_collider,
                    collider_offset,graphic_offset
            );
            
            if (max_vel == null)
            {
                max_vel = new Point(0,0);
            }
            
            maxVelX = max_vel.x;
            maxVelY = max_vel.y;
            
            velX = 0;
            velY = 0;
            
        }
        
        public function get velX() : Number
        {
            return _velX;
        }
        public function set velX(new_vel : Number) : void
        {
            if (maxVelX == 0)
            {
                // No limit
                _velX = new_vel;
            }
            else if (new_vel < -maxVelX)
            {
                // Too far into the negative
                _velX = -maxVelX;
            }
            else if (new_vel > maxVelX)
            {
                // Too far into the positive
                _velX = maxVelX;
            }
            else
            {
                // Within the valid range
                _velX = new_vel;
            }
        }
        
        public function get velY() : Number
        {
            return _velY;
        }
        public function set velY(new_vel : Number) : void
        {
            if (maxVelY == 0)
            {
                // No limit
                _velY = new_vel;
            }
            else if (new_vel < -maxVelY)
            {
                // Too far into the negative
                _velY = -maxVelY;
            }
            else if (new_vel > maxVelY)
            {
                // Too far into the positive
                _velY = maxVelY;
            }
            else
            {
                // Within the valid range
                _velY = new_vel;
            }
        }
        
        public function get maxVelX() : Number
        {
            return _maxVelX;
        }
        public function set maxVelX(new_max_vel : Number) : void
        {
            // Our velocity limits are
            // always positive
            _maxVelX = Math.abs(new_max_vel);
        }
        
        public function get maxVelY() : Number
        {
            return _maxVelY;
        }
        public function set maxVelY(new_max_vel : Number) : void
        {
            // Our velocity limits are
            // always positive
            _maxVelY = Math.abs(new_max_vel);
        }
        
        
        public function doMove() : void
        {
            collider.x += velX;
            collider.y += velY;
            
            
            for each (var obj : * in world.getObjectsForCollision(this,"Solid"))
            {
                if (obj is SpudEntity)
                {
                    var other_entity : SpudEntity = SpudEntity(obj);
                    
                    if (collider.overlaps(other_entity.collider))
                    {
                        collider.clampOff(other_entity.collider);
                    }
                }
            }
            
        }
        
        
        override public function update() : void
        {
            super.update();
            
            doMove();
            
        }
        
    }
    
}
